package edu.uc.ui.ctrl25;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.liuvei.common.PagerItem;
import com.liuvei.common.SysFun;

import edu.uc.bean.Logger;
import edu.uc.service.LoggerService;

@Repository("LoggerAction")
@Scope("prototype")
public class LoggerAction extends BaseAction {
	
	@Autowired
	private LoggerService loggerService;
	private Long id;
	private String type;
	private String message;
	private Date insertData;
	
	public String list(){
		List<Logger> vList = null;
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		Long rowCount = loggerService.count();
		pagerItem.changeRowCount(rowCount);
		vList = loggerService.pager(pagerItem.getPageNum(),pagerItem.getPageSize());
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		for(int i = 0; i<vList.size();i++){
			System.out.println(vList.get(i).getMessage());
		}
		return "list";
	} 
	
	public String listDeal() throws UnsupportedEncodingException{
		
		String str = new String(message.getBytes("iso-8859-1"),"UTF-8");
		request.put("message", str);
		if(!SysFun.isNullOrEmpty(str)){
			message = str.trim();
		}
		else{
			message = "";
		}
		System.out.println(message);
		List<Logger> vList = null;
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		
		Long rowCount = loggerService.countByName(message);
		pagerItem.changeRowCount(rowCount);
		
		vList = loggerService.pagerByName(message,pagerItem.getPageNum(),pagerItem.getPageSize());
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		return "list";
	}
	
	public String insert(){
		return "insert";
	}
	public String insertDeal() throws IOException{
		String vMsg="";
		Logger bean = new Logger();
		bean.setInsertData(new Date());
		bean.setMessage(message);
		bean.setType("javaee");
		Long result = 0L;
		try{
			result = loggerService.insert(bean);
		}
		catch (Exception e) {
			// TODO: handle exception
			vMsg +="添加失败"+e.getMessage();
		}
		if(result >0){
			return "go_preload";
		}else{
			request.put("msg", vMsg);
			return "insert";
		}
	}
	public String delete(){
		return "go_ok";
	}
	public String deleteDeal(){
		if(!SysFun.isNullOrEmpty(String.valueOf(id))){
			Long result = loggerService.delete(id);
			if(result >0){
				return "go_ok";
			}
		
		}
		return "go_no";
	}
	
	public String update(){
		if(!SysFun.isNullOrEmpty(String.valueOf(id))) {
			Logger bean = loggerService.load(id);
			if(bean != null) {
				request.put("id", bean.getId());
				request.put("type", bean.getType());
				request.put("message", bean.getMessage());
				request.put("insertData", bean.getInsertData());
				return "update";	
			}
		}
		return "update";
	}
	public String updateDeal(){
		Long result = 0L;
		String vMsg="";
		request.put("id", id);
		request.put("message", message);
		if(SysFun.isNullOrEmpty(message)){
			vMsg+="日志内容不能为空";
		}
		
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		Logger bean = loggerService.load(id);
		if(bean == null){
			vMsg +="没有找到相关数据";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		bean.setId(id);
		bean.setInsertData(bean.getInsertData());
		bean.setMessage(message);
		bean.setType(bean.getType());
		try{
			result = loggerService.update(bean);
		}
		catch (Exception e) {
			vMsg +="更新失败"+e.getMessage();
		}
		if(result >0){
			return "go_preload";
		}else{
			request.put("msg", vMsg);
			return "update";
		}
	}
	
	public String detail(){
		
		if(!SysFun.isNullOrEmpty(String.valueOf(id))) {
			String vMsg="";
			Logger bean = loggerService.load(id);
			if(bean != null) {
				request.put("id", bean.getId());
				request.put("type", bean.getType());
				request.put("message", bean.getMessage());
				request.put("insertData", bean.getInsertData());
				return "detail";	
			}
			else{
				vMsg+="查询失败";
				request.put("msg", vMsg);
			}
		}
		return "detail";

	}
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Date getInsertData() {
		return insertData;
	}
	public void setInsertData(Date insertData) {
		this.insertData = insertData;
	}
}
